const db = require('../db/index.js')
const util = require('util')
const query = util.promisify(db.query).bind(db)

// 获取新闻列表
exports.getrumorsRouter = async (req, res) => {
    let { query: searchName, pagenum, pagesize, startdate, enddate } = req.query;
    let start = (pagenum - 1) * pagesize; //pagenum=2  pagesize=1
    let length = pagesize;
    let sql1
    let sql2
    let sqlerr
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    if (searchName && startdate) {
        sql1 = `select * from covid19_rumors where title like '%${searchName}%' and DATE_FORMAT(pubDate,'%Y-%m-%d') BETWEEN '${startdate}' AND '${enddate}' limit ${start},${length}`
        sql2 = `select * from covid19_rumors where title like '%${searchName}%' and DATE_FORMAT(pubDate,'%Y-%m-%d') BETWEEN '${startdate}' AND '${enddate}'`
    } else if (!searchName && startdate) {
        sql1 = `select * from covid19_rumors where DATE_FORMAT(pubDate,'%Y-%m-%d') BETWEEN '${startdate}' AND '${enddate}' limit ${start},${length}`
        sql2 = `select * from covid19_rumors where DATE_FORMAT(pubDate,'%Y-%m-%d') BETWEEN '${startdate}' AND '${enddate}'`
    } else if (searchName && !startdate) {
        sql1 = `select * from covid19_rumors where title like '%${searchName}%' limit ${start},${length}`
        sql2 = `select * from covid19_rumors where title like '%${searchName}%'`
    } else {
        sql1 = `select * from covid19_rumors limit ${start},${length}`
        sql2 = `select * from covid19_rumors`
    }
    // if (searchName) {
    //     sql1 = `select * from covid19_rumors where title like '%${searchName}%' limit ${start},${length}`
    //     sql2 = `select * from covid19_rumors where title like '%${searchName}%'`
    // } else {
    //     sql1 = `select * from covid19_rumors limit ${start},${length}`
    //     sql2 = `select * from covid19_rumors`
    // }
    try {
        let results = await query(sql2)
        obj.data.total = results.length
        obj.data.pagenum = pagenum
    } catch (err) {
        sqlerr = err;
    }
    try {
        let results = await query(sql1)
        for (var i = 0; i < results.length; i++) {
            results[i] = {
                id: results[i].id,
                pubDate: results[i].pubDate,
                title: results[i].title,
                summary: results[i].summary,
                body: results[i].body,

            }
        }
        obj.data.rumors = results
    } catch (err) {
        sqlerr = err;
    }

    if (sqlerr) {
        console.log(sqlerr)
        obj.meta.status = 500;
        obj.meta.msg = "数据库查询错误";
        return res.send(obj);
    } else {
        obj.meta.status = 200;
        obj.meta.msg = "获取列表成功";
        return res.send(obj);
    }
}

exports.inquirerumorsById = (req, res) => {
    let id = req.params.id;
    const query1 = `select * from covid19_rumors WHERE id = ?`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, id, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length === 1) {
                var data1 = {
                    "id": results[0].id,
                    "pubDate": results[0].pubDate,
                    "title": results[0].title,
                    "summary": results[0].summary,
                    "body": results[0].body,

                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data = data1;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.deleterumorsRouter = (req, res) => {
    let id = req.params.id
    let sql1 = 'delete from covid19_rumors where id=?'
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(sql1, id, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        } else {
            if (results.affectedRows == 1) {
                obj.meta.status = 200;
                obj.meta.msg = '删除成功';
                return res.send(obj);
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '没有查询到数据或数据库内部错误';
                return res.send(obj);
            }
        }
    })
}

exports.addrumorsRouter = (req, res) => {
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    let { id, pubDate, title, summary, body } = req.body;
    let param1 = {
        id, pubDate, title, summary, body
    }
    let sql1 = "insert into covid19_rumors set ?"
    db.query(sql1, param1, (err, results) => {
        if (err) {
            console.log(err)
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        } else {
            if (results.affectedRows == 1) {
                obj.meta.status = 200;
                obj.meta.msg = '添加成功';
                obj.data = results;
                return res.send(obj);
            }
        }
    })
}

exports.updaterumorsRouter = (req, res) => {
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    let id = req.params.id;
    let { pubDate, title, summary, body } = req.body
    let changeValue = {
        pubDate: pubDate,
        title: title,
        summary: summary,
        body: body,
    }
    // console.log(req)
    let sql1 = "update covid19_rumors set ? where id = ?"
    db.query(sql1, [changeValue, id], (err, results) => {
        if (err) {
            console.log(err)
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        } else {
            if (results.affectedRows == 1) {
                obj.meta.status = 200;
                obj.meta.msg = '更新成功';
                return res.send(obj);
            }
        }
    })
}